import React, { Component } from 'react'
import { createRef } from 'react';
import style from  './style/style.module.css'
export default class App extends Component {
  scrollSize = createRef()
  state={
    newList:[]
  }
  
  componentDidMount(){
    setInterval(() => {
      const {newList} = this.state
      const news = `新闻${newList.length+1}`;
      this.setState({newList:[news,...newList]})
    }, 1000);
  }

  getSnapshotBeforeUpdate(){
    return this.scrollSize.current.scrollHeight
  }

  componentDidUpdate(preProps,preState,snapshotValue){
    
    this.scrollSize.current.scrollTop +=this.scrollSize.current.scrollHeight -snapshotValue
    console.log(this.scrollSize.current.scrollHeight);
  }

  render() {
    return (
      <div className={style.newsList} ref={this.scrollSize}>
        <ul>
          {this.state.newList.map((item,index)=>{
            return <li key={index}>{item}</li>
          })}
        </ul>
      </div>
    )
  }
}
